package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.email.SendEmailService;
import com.lsh.oms.core.enums.OrderStatus;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.MisProxy;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.utils.DateUtil;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author miaozhuang
 * @date 18/5/16
 * 订单完结通知mis
 */
@Slf4j
@Component
public class OrderFinishNotifyMisTask extends OmsSimpleElasticJob {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private MisProxy misProxy;

    @Override
    protected Integer getFetchSize() {
        return 50;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.ORDER_COUPON;
    }

    @Override
    protected List<Integer> dealTaskStatus() {

        return Arrays.asList(TaskStatus.TASK_UN_DEAL.getCode(), TaskStatus.TASK_DEAL_FAIL.getCode());
    }

    @Override
    protected int process(OmsTask omsTask) {
        Long orderCode = Long.valueOf(omsTask.getRefCode());

        OrderHead orderHead = this.orderQueryService.findByCode(orderCode, false, false);

        if (orderHead == null) {
            log.info("订单完结通知mis orderCode {}  orderHead is null ", orderCode);
            return -1;
        }

        if (!this.validationOrderStatus(orderHead)) {
            return 0;
        }

        log.info("订单完结通知mis 开始 {}", orderCode);
        JSONObject resultJson = this.misProxy.orderFinishNotify(orderCode, orderHead.getVenderId());

        if (resultJson == null) {

            this.sendEmail(orderCode, " 请求mis，返回结果为空");
            return -1;
        }
        Integer ret = resultJson.getInteger("ret");

        if (ret == null || 0 != ret) {

            String msg = resultJson.getString("msg") + " 请求失败 ";
            this.sendEmail(orderCode, msg);
            return -1;
        }

        return 1;
    }

    private Boolean validationOrderStatus(OrderHead head) {

        if (OrderStatus.ORDER_SIGN.getIndex() <= head.getOrderStatus()) {
            return true;
        }

        return false;
    }

    /**
     * 处理失败邮件发送
     *
     * @param orderCode 签收单号
     * @param msg       错误信息
     */
    private void sendEmail(Long orderCode, String msg) {
        if (msg.length() > 200) {
            msg = msg.substring(0, 200);
        }
        String message = orderCode + " 订单完成通知mis失败," + msg;

        this.sendEmailService.asyncSendEmail(message, "订单完成通知mis失败-" + DateUtil.formatTime(new Date()));
    }

}
